package com.zhu.ttwords.model;

import android.util.Log;

import com.zhu.ttwords.TTWORDS;
import com.zhu.ttwords.bean.WordBean;
import com.zhu.ttwords.bean.WordDataBean;
import com.zhu.ttwords.common.DBTool;
import com.zhu.ttwords.common.DateTool;
import com.zhu.ttwords.common.SuperMeMoUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by KnightYe on 2015/4/6.
 * 主要实现载入学习单词组的功能。
 */
public class StudyModel implements IStudyModel {

    /**
     * 加载学习数据。新词，在user学习词库中没有的单词
     *
     * @return 返回学习用词组
     */
    @Override
    public List<Object> loadStudyWord() {
        List<Object> mData = new ArrayList<>();
        String[] param = new String[4];
        param[0] = TTWORDS.getAppSetting().getBook() + "";
        param[1] = TTWORDS.getAppSetting().getBook() + "";
        param[2] = TTWORDS.getAppSetting().getUserId();
        param[3] = TTWORDS.getAppSetting().getGroup() + "";
        try {
            mData = DBTool.getDataBean(WordBean.class, TTWORDS.SQL_STUDY_INIT_STUDY, param);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return mData;
    }

    /**
     * 加载复习单词，user词库中今天需要复习的单词
     *
     * @return 返回复习用词组。
     */
    @Override
    public List<Object> loadReviewWord() {
        List<Object> mData = new ArrayList<>();
        String[] param = new String[2];
        //当前日期
        param[0] = DateTool.getCurrentDateTime();
        //书ID
        param[1] = TTWORDS.getAppSetting().getGroup() + "";
        try {
            mData = DBTool.getDataBean(WordBean.class, TTWORDS.SQL_STUDY_INIT_REVIEW, param);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return mData;
    }

    /**
     * 加载测试数据。
     *
     * @return 返回测试用词组。
     */
    @Override
    public List<Object> loadExamWord() {
        List<Object> mData = new ArrayList<>();
        String[] param = new String[4];
        param[0] = TTWORDS.getAppSetting().getBook() + "";
        param[1] = TTWORDS.getAppSetting().getBook() + "";
        param[2] = TTWORDS.getAppSetting().getUserId();
        param[3] = TTWORDS.getAppSetting().getGroup() + "";
        try {
            mData = DBTool.getDataBean(WordBean.class, TTWORDS.SQL_STUDY_INIT_STUDY, param);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return mData;
    }

    @Override
    public void saveWordResult(WordBean bean, boolean result, int level) {
        List<Object> mData;
        WordDataBean wbean;
        String[] param = new String[3];
        param[0] = TTWORDS.getAppSetting().getBook() + "";
        param[1] = TTWORDS.getAppSetting().getUserId();
        param[2] = bean.getWid() + "";
        try {
            mData = DBTool.getDataBean(WordDataBean.class, TTWORDS.SQL_EXAM_SAVE_CHECK, param);
            if (mData.size() > 0) {//判断是否是第一次学习。
                //如果有学习记录，说明不是第一次学习。
                wbean = (WordDataBean) mData.get(0);
                if (result) {
                    wbean.setCount_right(wbean.getCount_right() + 1);
                } else {
                    wbean.setCount_wrong(wbean.getCount_wrong() + 1);
                }
                wbean.setWid(bean.getWid());
                wbean.setUid(TTWORDS.getAppSetting().getUserId());
                wbean.setLevel(level);
                wbean.setUpdate_date(DateTool.getCurrentDateTime());
                wbean.setDUR(SuperMeMoUtil.getDay(wbean.getDUR(), wbean.getEF(), wbean.getN()));
                wbean.setEF(SuperMeMoUtil.getEM(wbean.getEF(), level));
                if (level < 3) {
                    wbean.setN(1);
                } else {
                    wbean.setN(wbean.getN() + 1);
                }
                wbean.setNext_date(DateTool.addOnCurrentDateTime(wbean.getDUR()));
            } else {
                //第一次学习
                wbean = new WordDataBean();
                if (result) {
                    wbean.setCount_right(wbean.getCount_right() + 1);
                } else {
                    wbean.setCount_wrong(wbean.getCount_wrong() + 1);
                }
                wbean.setUid(TTWORDS.getAppSetting().getUserId());
                wbean.setWid(bean.getWid());
                wbean.setUpdate_date(DateTool.getCurrentDateTime());
                wbean.setCreate_date(DateTool.getCurrentDateTime());
                wbean.setN(1);
                wbean.setEF(2.5f);
                wbean.setLevel(level);
                wbean.setDUR(SuperMeMoUtil.getDay(wbean.getDUR(), wbean.getEF(), wbean.getN()));
                wbean.setEF(SuperMeMoUtil.getEM(wbean.getEF(), level));
                if (level < 3) {
                    wbean.setN(1);
                }
                wbean.setNext_date(DateTool.addOnCurrentDateTime(wbean.getDUR()));
            }
            long resultIndex = DBTool.saveBeanData(wbean);
            Log.d("StudyModel", "replace index ID:" + resultIndex);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //todo 算法结束后做
    }
}
